home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Development Tools & Languages / Macintosh Common Lisp Related / Think C interface / example.lisp < prev    next >
Encoding:
Text File  |  1994-09-12  |  4.5 KB  |  170 lines  |  [TEXT/CCL2]

  1. ;;; -*- package: CL-USER -*-
  2. ;;;
  3. ;;;  Example for the MCL <-> ThinkC interface
  4. ;;;
  5.  
  6.  
  7. (in-package "CL-USER")
  8. (use-package "THINK-C")
  9.  
  10.  
  11. (defvar *resource-file*
  12.   nil)
  13.  
  14.  
  15. (defcmodule example
  16.   ;;
  17.   ;; If you do not specify a resource file, it defaults to
  18.   ;; (merge-pathnames *THINK-C-FOLDER* (symbol-name module-name)).
  19.   ;;
  20.   :resource-file
  21.   (or *resource-file*
  22.       (setq *resource-file*
  23.             (progn
  24.               (message-dialog "Please select the 'example.res' file.")
  25.               (choose-file-dialog))))
  26.   :variables
  27.   (my-long
  28.    my-double)
  29.   :functions
  30.   ((test-fixnum        (:lisp)  :d0)
  31.    (test-character     (:lisp)  :d0)
  32.    ;;
  33.    ;; If you want your arguments to have more meaningfull names
  34.    ;; you can use this alternate syntax for the arguments.
  35.    ;;
  36.    (test-list          ((list :lisp)) :novalue)
  37.    (test-struct        (:lisp)  :novalue)
  38.    (test-vector        (:lisp)  :novalue)
  39.    (test-string        (:lisp)  :novalue)
  40.    ;;
  41.    ;; The ThinkC type that corresponds to MCL's floating point
  42.    ;; is short double. I provided a way to pass ThinkC doubles
  43.    ;; between C and MCL because this is the type of the 6888*.
  44.    ;; So if you badly need efficiency you can use this.
  45.    ;;
  46.    (test-short-double  (:lisp)  :novalue)
  47.    (test-double        (:mac)   :novalue)
  48.    (test-c-structures  (:mac)   :novalue)
  49.    (test-a0            (:mac)   :a0)
  50.    ;;
  51.    ;; If you want ThinkC to make call backs to Lisp you have to be
  52.    ;; carefull because a callback can give rise to a GC which can
  53.    ;; invalidate the pointers that C has in the Lisp world.
  54.    ;; Gary Byers pointed out to me that even if you dont make any
  55.    ;; call backs to Lisp, a request to the macintosh memory manager
  56.    ;; can possibly result in triggering a MCL garbage collect!
  57.    ;;
  58.    ;; Your C code can make callbacks to Lisp because it only gets
  59.    ;; pointers to the actual arguments, those pointers being updated
  60.    ;; automatically in case of a garbage collection.
  61.    ;;
  62.    (test-callback      (:lisp :mac) :d0)
  63.    (test-globals       () :d0)
  64.    (test-multi-segment () :d0)
  65.    (test-traps         () :a0)))
  66.  
  67.  
  68. ;;
  69. ;; LOAD-CMODULE reads the main segment of your code resource in memory
  70. ;; and put the address in the symbol naming your C module (here EXAMPLE).
  71. ;; 
  72. ;; LOAD-CMODULE will also link the variables and functions you defined in
  73. ;; your C module with their correct address in memory. The symbol value of
  74. ;; the variables will containt the address of their C counterparts and the
  75. ;; symbol value of the functions will contain their entry point address.
  76. ;;
  77. ;; It is important to understand that MCL doesnt conserve the Mac heap
  78. ;; accross a SAVE-APPLICATION. This implies that in order for your C modules
  79. ;; to be restored properly, the resource file must still be present even
  80. ;; after a SAVE-APPLICATION.
  81. ;;
  82. (load-cmodule  'example)
  83.  
  84. ;;
  85. ;; CLOSE-CMODULE closes the resource file associated with your C module.
  86. ;; It is very important if your C module is multi-segmented, that you do
  87. ;; not close it before you are through using it, as a call to an unloaded
  88. ;; segment will force the segment loader to load it from your resource file.
  89. ;;
  90. (close-cmodule 'example)
  91.  
  92.  
  93. ;;
  94. ;; A typical debugging cycle for your C code will be the following:
  95. ;; You evaluate your DEFCMODULE form only once.
  96. ;; After that, you load your C module with LOAD-CMODULE, test it,
  97. ;; close it with CLOSE-CMODULE, go back to ThinkC, make changes to your code,
  98. ;; rebuild the code resource, and the cycles repeats itself.
  99. ;;
  100.  
  101.  
  102. #|
  103. (test-fixnum 10)
  104.  
  105. (test-character #\a)
  106. (test-character #\b)
  107. (test-character #\h)
  108.  
  109. (setq l '(1 6 7 8 9))
  110. (test-list l)
  111. l
  112.  
  113. (defstruct foo a b c)
  114. (setq s (make-foo :a 10 :b 20 :c 30))
  115. (test-struct s)
  116. s
  117.  
  118. (setq a (make-array 5 :initial-contents '(1 2 3 4 5)))
  119. (test-vector a)
  120. a
  121.  
  122. (setq s "hello how are yah")
  123. (test-string s)
  124. s
  125.  
  126. (setq a 2.3)
  127. (test-short-double a)
  128. a
  129.  
  130. (setq a (%make-double 2.3))
  131. (test-double a)
  132. (%get-double a)
  133.  
  134. (defrecord :myrecord
  135.   (a :long)
  136.   (b :long)
  137.   (c :long))
  138. (setq a (make-record :myrecord :a 3 :b 4 :c 8))
  139. (test-c-structures a)
  140. (print-record a :myrecord)
  141.  
  142. (setq p (#_newPtr 10))
  143. (test-a0 p)
  144.  
  145. ;;
  146. ;; Dont worry about the warning
  147. ;; it should not be there in MCL2.0 final!
  148. ;;
  149. (defccallable gc ((:result :void)) (gc))
  150. (test-callback '(2 3 4) gc)
  151.  
  152. (test-globals)
  153.  
  154. ;;
  155. ;; If you inadvertently evaluated the CLOSE-CMODULE form
  156. ;; in the middle of this file, it is not to late to reload your
  157. ;; module with the LOAD-CMODULE form, before it crashes with an
  158. ;; error of type 15!
  159. ;;
  160. (test-multi-segment)
  161.  
  162. (test-traps)
  163.  
  164. (%get-long my-long)
  165. (%get-double my-double)
  166.  
  167. (setf (%get-long my-long) 7)
  168. (test-globals)
  169. |#
  170.